package com.leet.code.simple_array;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/11/24 21:01
 */

public class ArrayDegree {

    public int findShortestSubArray(int[] nums) {
        HashMap<Integer, Integer> left = new HashMap();
        HashMap<Integer, Integer> right = new HashMap();
        HashMap<Integer, Integer> count = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            count.put(nums[i], count.getOrDefault(nums[i], 0) + 1);
            if (left.get(nums[i]) == null) {
                left.put(nums[i], i);
            } else {
                right.put(nums[i], i);
            }
        }
        int degree = 0;
        for (Map.Entry entry : count.entrySet()) {
            degree = degree > ((Integer) entry.getValue()) ? degree : ((Integer) entry.getValue());
        }
        int res = Integer.MAX_VALUE;
        for (Map.Entry entry : count.entrySet()) {
            if (degree == ((Integer) entry.getValue())) {
                res = (right.get(entry.getKey()) - left.get(entry.getKey())) > res ? res : (right.get(entry.getKey()) - left.get(entry.getKey()));
            }
        }
        return res;
    }
}
